package com.geralt.im.jsonparser.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geralt.im.jsonparser.IjsonParser;

public class ParameterJsonParser extends JsonDeserializer<Map<String, Object>> {

	
	private static Map<String, String> map = new HashMap<String, String>();
	static {
		try {
			InputStream is = IjsonParser.class.getClassLoader()
					.getResourceAsStream("requetJsonParse.properties");
			Properties property = new Properties();
			property.load(is);
			Set<Object> kSet = property.keySet();
			for (Object key : kSet) {
				map.put((String) key, property.getProperty((String) key));
			}
//			System.out.println("Map:"+map);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public Map<String, Object> deserialize(JsonParser p,
			DeserializationContext ctxt) throws IOException,
			JsonProcessingException {
		
		Map<String, Object> map = new HashMap<String, Object>();
		ObjectCodec oc = p.getCodec();
		JsonNode node = oc.readTree(p);
		System.out.println("@@@" + node);
		ObjectMapper mapper = new ObjectMapper();
		Iterator<String> iterator = node.fieldNames();
		while (iterator.hasNext()) {
			String key = iterator.next();
//			if ("user".equals(key)) {
//				map.put(key, mapper.treeToValue(node.get(key), MyUser.class));
//			} else if ("date".equals(key)) {
//				map.put(key, mapper.treeToValue(node.get(key), Date.class));
//			}

		}
		return map;
	}

}
